package cn.kgc.web.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: 课工场
 * @since 2023/9/27
 */
public class MybatisUtil {

    private static SqlSessionFactory factory;

    static {
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取指定接口的代理对象
    //public static Object getMapper(Class clazz){
    //
    //    SqlSession sqlSession = factory.openSession(true);
    //    Object mapper = sqlSession.getMapper(clazz);
    //    return mapper;
    //}

    public static <T> T getMapper(Class<T> clazz){

        SqlSession sqlSession = factory.openSession(true);
        T mapper = sqlSession.getMapper(clazz);
        return mapper;
    }


}
